/*
 * 深圳市灵智数科有限公司版权所有.
 */
package com.spring.bean.definition;

import com.spring.bean.definition.factory.DefaultUserFactory;
import com.spring.bean.definition.factory.UserFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;

/**
 * Bean初始化
 *
 * @author 程健
 * @version 1.0.0
 * @date 2020/12/10
 */
public class BeanInitializationDemo {

    public static void main(String[] args) {

        //创建BeanFactory IOC
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        //注册 Configuration Class（配置类）
        applicationContext.register(BeanInitializationDemo.class);

        //启动spring 上下文
        applicationContext.refresh();

        // 非延迟初始化在 Spring 应用上下文启动完成后，被初始化
        System.out.println("Spring 应用上下文已启动...");

        UserFactory userFactory = applicationContext.getBean(UserFactory.class);

        System.out.println(userFactory);

        //关闭应用上下文
        applicationContext.close();

        System.out.println("应用上下文已关闭");
    }

    @Bean(initMethod = "initUserFactory", destroyMethod = "doDestroy")
    @Lazy(value = false)
    public UserFactory userFactory() {
        return new DefaultUserFactory();
    }

}
